import ConstStrings from "./datas/strings";
import ScreenPositionUtil from "./ScreenPositionUtil";

export default class SelectItemView extends Laya.Box {
    constructor() {
        super();
        const Image = Laya.Image;

        let viewSelectItemWidthRatio = 0.13;
        let viewSelectItemImgWHRatio = 0.72;

        this.WID = ScreenPositionUtil.getPostionInStage(viewSelectItemWidthRatio, "x");
        this.HEI = this.WID / viewSelectItemImgWHRatio;
        this.size(this.WID, this.HEI);

        // 半透白底
        this.bgImg = new Image();
        this.bgImg.size(this.WID, this.HEI);
        this.addChild(this.bgImg);
        this.bgImg.skin = ConstStrings.selectedViewItemSelecting;
        this.bgImg.alpha = 0;

        // 选中过度背景img 
        this.selectBgImg = new Image();
        this.selectBgImg.size(this.WID, this.HEI);
        this.addChild(this.selectBgImg);
        this.selectBgImg.skin = ConstStrings.selectedViewItemBg;

        // 前景img  鞋子
        this.srcImg = new Image();
        this.srcImg.size(this.WID * 0.99, this.WID * 0.99);
        this.srcImg.pivot(this.WID / 2, this.WID / 2);
        this.srcImg.pos(this.WID / 2, this.HEI / 2);
        this.srcImg.rotation = -30;

        this.addChild(this.srcImg);
        this.srcImg.alpha = 0;
    }

    setSrcImg(src, isFirstEmpty) {
        if (src) {
            this.srcImg.skin = src;
            this.selectBgImg.skin = ConstStrings.selectedViewItemSelectBg;

            Laya.Tween.to(this.srcImg, { alpha: 1 }, 300, Laya.Ease.quartOut, null, 300, true);
            Laya.Tween.to(this.selectBgImg, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 300, true);
            // Laya.Tween.to(this.bgImg, {alpha: 1},300,Laya.Ease.quartOut , null , 300,true);

        }
        else {
            let _self = this;
            Laya.timer.once(500, this, function () {
                _self.srcImg.skin = null;
                _self.selectBgImg.skin = isFirstEmpty ? ConstStrings.selectedViewItemSelectBg : ConstStrings.selectedViewItemBg;
                Laya.Tween.to(_self.selectBgImg, { alpha: 1 }, 300, Laya.Ease.quartOut, null, 0, true);
                Laya.Tween.to(_self.srcImg, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 0, true);
            }, null);
        }
    }

    getSelectAniInfo() {
        let gPos = this.localToGlobal(new Laya.Point(this.srcImg.x - this.srcImg.width / 2, this.srcImg.y - this.srcImg.height / 2))
        return {
            x: gPos.x,
            y: gPos.y,
            width: this.WID * 0.9,
            height: this.WID * 0.9
        }
    }
}